package com.zmt.mrms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmt.mrms.entity.MedicalRecord;
import com.zmt.mrms.entity.PaymentRecord;
import com.zmt.mrms.service.PatientService;

import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Controller
public class PatientNotLoggedController {

	@Resource
	PatientService patientService;

	private HttpServletRequest request;
	private HttpServletResponse response;

	protected PatientNotLoggedController(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	@GetMapping("/registered")
	public String fpRegistered1(@RequestParam(value = "type", required = false) String type, Model model) {
		model.addAttribute("type", patientService.getSectionList().get("type"));
		if (type != null) {
			model.addAttribute("section", patientService.getSectionList().get(type));
			return "patient/fpRegistered::sectionList";
		} else {
			return "patient/fpRegistered";
		}

	}

	@GetMapping("/patient/registered")
	public String fpRegisteredisLogged(@RequestParam(value = "type", required = false) String type, Model model,
			@CookieValue(value = "id") String id) {
		model.addAttribute("user", patientService.getPersonalInfo(id));
		model.addAttribute("type", patientService.getSectionList().get("type"));
		if (type != null) {
			model.addAttribute("section", patientService.getSectionList().get(type));
			return "patient/fpRegistered::sectionList";
		} else {
			return "patient/fpRegistered";
		}

	}

	@PostMapping("/registered")
	public String fpRegistered2(MedicalRecord medicalRecord) {
		String result = patientService.doRegistered(medicalRecord);
		Cookie cookie = new Cookie("unpaidBill", result);
		cookie.setHttpOnly(true);
		cookie.setPath("/");
		this.response.addCookie(cookie);
		return "redirect:/payment";
	}

	@GetMapping("/payment")
	public String fpPayment(Model model, @CookieValue(value = "unpaidBill", required = false) String paymentId) {
//		System.out.println(patientService.getPaymentList(Integer.parseInt(paymentId)));
		if (paymentId == null) {
			model.addAttribute("unpaidBill", false);
		} else {
			PaymentRecord paymentBill = patientService.getPayment(Integer.parseInt(paymentId));
			if (paymentBill.getRegisteredCost() != 0.0 && !paymentBill.isRegisteredStatus()) {
				model.addAttribute("billName", "挂号费-即医师诊费：");
				model.addAttribute("price", paymentBill.getRegisteredCost());
				model.addAttribute("billType", "registered_status");
				model.addAttribute("unpaidBill", true);
			} else if (paymentBill.getMedicineCost() != 0.0 && !paymentBill.isMedicineStatus()) {
				model.addAttribute("billName", "医药费：");
				model.addAttribute("price", paymentBill.getMedicineCost());
				model.addAttribute("billType", "medicine_status");
				model.addAttribute("unpaidBill", true);
			} else {
				model.addAttribute("unpaidBill", false);
			}
		}
		return "patient/fpPayment";
	}

	@PostMapping("/payment")
	public String payForBill(Model model, String billType, @CookieValue(value = "unpaidBill") String paymentId) {
		String result = patientService.payForBill(billType, Integer.parseInt(paymentId));
		if (result.equals("success")) {
			if (billType.equals("medicine_status")) {
				Cookie cookie = new Cookie("unpaidBill", null);
				cookie.setMaxAge(0);
				cookie.setPath("/");
				this.response.addCookie(cookie);
			}
			model.addAttribute("success", "缴费成功");
		}
		return "redirect:/payment";
	}

	@GetMapping("/medicalRecord")
	public String fpMedicalRecord(Model model, @RequestParam(value = "medicalId", required = false) String medicalId,
			@RequestParam(value = "idcard", required = false) String idcard) {
		if (medicalId != null) {
			model.addAttribute("MedicalList", patientService.getMedRecordbyMedId(Integer.parseInt(medicalId)));
			return "patient/fpMedRecord::MedicalList";
		} else if (idcard != null) {
			model.addAttribute("MedicalList", patientService.getMedRecordbyIdcard(idcard));
			return "patient/fpMedRecord::MedicalList";
		} else {
			return "patient/fpMedRecord";
		}
	}

	@GetMapping("/prescription")
	public String fpPrescription(Model model, @RequestParam(value = "medicalId", required = false) String medicalId,
			@RequestParam(value = "idcard", required = false) String idcard) {
		if (medicalId != null) {
			System.out.println(patientService.getPreRecordbyMedId(Integer.parseInt(medicalId)));
			model.addAttribute("PrescriptionList", patientService.getPreRecordbyMedId(Integer.parseInt(medicalId)));
			return "patient/fpPrescription::PrescriptionList";
		} else if (idcard != null) {
			model.addAttribute("PrescriptionList", patientService.getPreRecordbyIdcard(idcard));
			return "patient/fpPrescription::PrescriptionList";
		} else {
			return "patient/fpPrescription";
		}

	}

	@GetMapping("/")
	public String indexPage(Model model, @CookieValue(value = "role", defaultValue = "null") String role) {
		Boolean flag = role.equals("null") ? false : true;
		if (flag) {
			if (role.equals("DOCTOR")) {
				return "redirect:/doctor";
			} else if (role.equals("ADMIN")) {
				return "redirect:/admin";
			} else {
				model.addAttribute("loginStatus", flag);
				return "redirect:/patient";
			}
		} else {
			model.addAttribute("loginStatus", flag);
			return "homepageFP";
		}
	}
}
